package top.whysu.gps.service.navigation.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.whysu.gps.constant.CodeConstant;
import top.whysu.gps.constant.SystemConfigKey;
import top.whysu.gps.dao.gps.NavigationDao;
import top.whysu.gps.dao.gps.NavigationRecordDao;
import top.whysu.gps.dto.navigation.LastSendDataQueryDTO;
import top.whysu.gps.dto.navigation.NavigationRecordAddDTO;
import top.whysu.gps.exception.BusinessException;
import top.whysu.gps.po.navigation.NavigationPO;
import top.whysu.gps.po.navigation.NavigationRecordPO;
import top.whysu.gps.service.navigation.NavigationRecordService;
import top.whysu.gps.service.system.AccountService;
import top.whysu.gps.service.system.SystemConfigService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 航行记录表
 */
@Service("NavigationRecordServiceImpl")
public class NavigationRecordServiceImpl implements NavigationRecordService {
    private static final Logger LOGGER = LoggerFactory.getLogger(NavigationRecordServiceImpl.class);

    @Resource
    private NavigationDao navigationDao;
    @Resource
    private NavigationRecordDao navigationRecordDao;
    @Resource
    private AccountService accountService;
    @Resource
    private SystemConfigService systemConfigService;

    @Override
    public List<NavigationRecordPO> getCurrentRecordList(BigDecimal navigationId) {
        if (navigationId == null) {
            throw new BusinessException(CodeConstant.Http.EXECUTE_FAIL, "请指定航行表！");
        }
        NavigationRecordPO queryPO = new NavigationRecordPO();
        queryPO.setNavigationId(navigationId);
        return navigationRecordDao.queryAll(queryPO);
    }

    @Override
    public List<NavigationRecordPO> getLimitRecordList(BigDecimal navigationId) {
        if (navigationId == null) {
            throw new BusinessException(CodeConstant.Http.EXECUTE_FAIL, "请指定航行表！");
        }
        return navigationRecordDao.queryLimit(navigationId, 5);
    }

    @Override
    public void addNavigationRecord(NavigationRecordAddDTO addDTO) {
        if (addDTO == null || addDTO.getNavigationId() == null) {
            throw new BusinessException(CodeConstant.Http.EXECUTE_FAIL, "addDTO以及id不能为空！");
        }
        // 查询航程表
        NavigationPO navigationPO = navigationDao.queryById(addDTO.getNavigationId());
        if (navigationPO == null) {
            throw new BusinessException(CodeConstant.Http.EXECUTE_FAIL, "航程表不存在，请刷新页面或者重启启动航程！");
        }
        NavigationRecordPO po = new NavigationRecordPO();
        BeanUtils.copyProperties(addDTO, po);
        po.setNavigationName(navigationPO.getNavigationName());
        // 当前操作用户以及时间
        String currentUser = accountService.getCurrentLoginUser();
        Date currentDate = new Date();
        po.setCreateTime(currentDate);
        po.setCreateBy(currentUser);
        po.setUpdateTime(currentDate);
        po.setUpdateBy(currentUser);
        navigationRecordDao.insert(po);
        // 更新航向
        systemConfigService.newTxUpdateConfig(SystemConfigKey.Common.COMMON_COURSE, addDTO.getCourse());
    }

    @Override
    public NavigationRecordPO selectLastSendData(LastSendDataQueryDTO queryDTO) {
        return navigationRecordDao.selectLastSendData(queryDTO);
    }

}